﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using Controller;

namespace UxNumberSorter
{
    public partial class uxNumberSortViewer : Form
    {
        #region Fields
        //  Fields

        #endregion

        #region Constructors
        //  Constructors

        public uxNumberSortViewer ( )
        {
            InitializeComponent ( );

            //  sub control events
            uxSetSequenceSize.Click += new EventHandler ( uxSetSequenceSize_Click );
            uxCallControllerSort.Click += new EventHandler(uxCallControllerSort_Click);
            uxCompletedSortsSelector.SelectedValueChanged += new EventHandler(uxCompletedSortsSelector_SelectedValueChanged);
            uxCheckViewSortedSeq.CheckedChanged += new EventHandler(uxCheckViewSortedSeq_CheckedChanged);
            uxShowUnsortedSequence.Click += new EventHandler(uxShowUnsortedSequence_Click);
            
            //  complete control configurations
            uxSortedSeqPanel.Visible = uxCheckViewSortedSeq.Checked;
            SetEnabledControls(false);
            UpdateSorterStatus( );
        }

        #endregion

        #region Functions
        //  Functions

        #region Event Handlers
        
        void uxSetSequenceSize_Click ( object sender, EventArgs e )
        {
            //  initialize collection
            InitializeCollection(uxSequenceSize.Text);

            //  initialize SortController
            SortController.CreateSorters(randoms);

            //  sub sort completed event
            SortController.OnSortCompleted += new EventHandler<SortCompletedEventArgs>(SortController_OnSortCompleted);
        }

        void uxShowUnsortedSequence_Click(object sender, EventArgs e)
        {
            DisplayUnsortedSequence( );
        }

        void uxCallControllerSort_Click(object sender, EventArgs e)
        {
            //  avoid additional sequence generation while sorting
            uxSetSequenceSize.Enabled = false;
            //  avoid multiple sort execution
            uxCallControllerSort.Enabled = false;

            //  begin SortController.StartSorters
            SortSequence( );
        }

        void uxCompletedSortsSelector_SelectedValueChanged(object sender, EventArgs e)
        {
            if (uxCompletedSortsSelector.SelectedValue == null)
            { return; }

            //  display selected sort info
            SelectSortInfo(uxCompletedSortsSelector.SelectedValue.ToString( ));
        }

        void uxCheckViewSortedSeq_CheckedChanged(object sender, EventArgs e)
        {
            //  set uxSortedPanel visibility
            uxSortedSeqPanel.Visible = uxCheckViewSortedSeq.Checked;

            //  if nothing selected, return
            if (uxCompletedSortsSelector.SelectedValue == null)
            { return; }

            //  display sorted sequence
            DisplaySortedSequence(uxCompletedSortsSelector.SelectedValue.ToString( ));
        }

        //  event handler - raised by SortController
        void SortController_OnSortCompleted(object sender, SortCompletedEventArgs e)
        {
            SortInfo sortInfo = (SortInfo)sender;

            //  check UI thread sync
            if (InvokeRequired)
            {
                //  UI thread un-synced - BeginInvoke
                BeginInvoke(new UpdateSorterSelector(UpdateCompletedSortSelector), sortInfo);
                return;
            }

            // UI thread synced
            UpdateCompletedSortSelector(sortInfo);
        }
        #endregion
        #endregion

        #region Properties
        //  Properties

        #endregion
    }
}
